package com.chzc.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chzc.po.Role;
import com.chzc.sys.req.RoleREQ;
import com.chzc.sys.mapper.RoleMapper;
import com.chzc.sys.service.IRoleService;
import com.chzc.util.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author CV大魔王
 * @since 2021-05-24
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Override
    @Cacheable(value = {"role"}, key = "#root.methodName+'_'+#root.method+'_userId_'+#userId")
    public List<String> findRoleCodeListByUserId(String userId) {
        return baseMapper.findRoleCodeListByUserId(userId);
    }

    @Override
    @Cacheable(value = {"role"}, key = "#root.methodName+'_'+#root.method+'_userId_'+#userId")
    public List<Role> findRoleListByUserId(String userId) {
        return baseMapper.findRoleListByUserId(userId);
    }


    @Override
    @Cacheable(value = {"role"}, key = "#root.methodName+'_'+#root.method+'_'+#req.name+'_'+#req.page+'_'+#req.size")
    public IPage<Role> queryPage(RoleREQ req) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        // 条件查询
        if (StringUtils.isNotEmpty(req.getName())) {
            wrapper.like("name", req.getName());
        }
        wrapper.orderByAsc("update_date");
        return baseMapper.selectPage(req.getPage(), wrapper);
    }

    @Cacheable(value = {"role"},  key = "#root.methodName+'_'+#root.method+'_'+#id")
    @Override
    public List<String> findMenuIdsById(String id) {
        return baseMapper.findMenuIdsById(id);
    }


    @Override
    @Caching(evict = {
            @CacheEvict(value = "role",allEntries = true),
            @CacheEvict(value = "user",allEntries = true),
            @CacheEvict(value = "menu",allEntries = true)
    })
    public Result saveRoleMenu(String roleId, List<String> menuIds) {
        // 1. 先删除角色菜单关系表数据
        baseMapper.deleteRoleMenuByRoleId(roleId);
        // 2. 再保存新的角色菜单关系表数据
        if (CollectionUtils.isNotEmpty(menuIds)) {
            baseMapper.saveRoleMenu(roleId, menuIds);
        }
        return Result.addSuccess();
    }

    @Caching(evict = {
            @CacheEvict(value = "role",allEntries = true),
            @CacheEvict(value = "user",allEntries = true),
            @CacheEvict(value = "menu",allEntries = true)
    })
    @Override
    public Result deleteById(String id) {
        // 1. 通过角色 id 删除角色信息表数据
        baseMapper.deleteById(id);
        // 2. 通过角色 id 删除角色菜单关系表数据
        baseMapper.deleteRoleMenuByRoleId(id);
        return Result.deleteSuccess();
    }
}
